package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"yigin/model"
    "github.com/gin-contrib/sessions"
)

func AdminIndex(c *gin.Context) {
	model.CloseDB()
	c.HTML(
		http.StatusOK,
		"admin.html",
		gin.H{},
	)
}

func Login(c *gin.Context) {
	session := sessions.Default(c)
	Username := c.Query("username")
	Password := c.Query("password")
	if session.Get("user") != nil {
		c.JSON(
            http.StatusOK,
            gin.H{
                "code":  http.StatusOK,
                "error": "Successful second visit",
            },
        )
		return
	}
	if Username == "admin" && Password == "123456" {
		
		session.Set("user", Username)
		session.Save()
		c.JSON(
            http.StatusOK,
            gin.H{
                "code":  http.StatusOK,
                "error": "Successful second visit",
            },
        )
	}else{
		c.JSON(
            http.StatusOK,
            gin.H{
                "code":  http.StatusOK,
                "error": "username or password is error",
            },
        )
	}
}

func LogOut(c *gin.Context) {
	session := sessions.Default(c)
	session.Delete("user")
	session.Save()
	c.JSON(
		http.StatusOK,
		gin.H{
			"code":  http.StatusOK,
			"error": "logout ok!",
		},
	)
}
